package Base;

public class MyArraylist {
    public static void main(String[] args) {
        MyArraylist myArraylist=new MyArraylist();
        for ( int i=0;i<11;i++ ){
            myArraylist.add(i);
        }
        myArraylist.display();
    }
    public int[] elem;
    public int usedSize;//0
    //默认容量
    private static final int DEFAULT_SIZE = 10;

    public MyArraylist() {
        this.elem = new int[DEFAULT_SIZE];
    }

    /**
     * 打印顺序表:
     * 根据usedSize判断即可
     */
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.println(this.elem[i]);
        }
    }

    // 新增元素,默认在数组最后新增
    public void add(int data) {
        //先判断此时数组是否已满，满了需要进行扩充
        if (this.usedSize == this.elem.length) {
            int[] arr = new int[this.usedSize];
            System.arraycopy(elem, 0, arr, 0, this.usedSize);
            this.elem = new int[this.usedSize + MyArraylist.DEFAULT_SIZE];
            System.arraycopy(arr, 0, this.elem, 0, this.usedSize);
        }
        this.elem[this.usedSize] = data;
        this.usedSize++;
    }

    /**
     * 判断当前的顺序表是不是满的！
     *
     * @return true:满   false代表空
     */
    public boolean isFull() {
        if (this.usedSize == this.elem.length) {
            return true;
        }
        return false;
    }


    private boolean checkPosInAdd(int pos) {
        if (pos >= this.usedSize - 1) {
            return false;
        }
        return true;//合法
    }

    // 在 pos 位置新增元素
    public void add(int pos, int data) {
        //满时需要扩容
        if (this.usedSize == this.elem.length) {
            int[] arr = new int[this.usedSize];
            System.arraycopy(elem, 0, arr, 0, this.usedSize);
            this.elem = new int[this.usedSize + MyArraylist.DEFAULT_SIZE];
            System.arraycopy(arr, 0, this.elem, 0, this.usedSize);
        }
        if (this.checkPosInAdd(pos)) {
            for (int i = this.usedSize; i > pos; i--) {
                this.elem[i] = this.elem[i - 1];
            }
            this.elem[pos] = data;
        }

    }

    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (this.elem[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    // 查找某个元素对应的位置
    public int indexOf(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (this.elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    // 获取 pos 位置的元素
    public int get(int pos) {
        if (this.checkPosInAdd(pos)) {
            return this.elem[pos - 1];
        }
        return 0;
    }

    private boolean isEmpty() {
        if (this.usedSize == 0) {
            return true;
        }
        return false;
    }

    // 给 pos 位置的元素设为【更新为】 value
    public void set(int pos, int value) {
        if (!checkPosInAdd(pos)) {
            return;
        }
        this.elem[pos] = value;
    }

    /**
     * 删除第一次出现的关键字key
     *
     * @param key
     */
    public void remove(int key) {
        //双指针法去除
        int left = 0;
        for (int right = 0; right < this.usedSize; right++) {
            if (this.elem[right] != key) {
                this.elem[left] = this.elem[right];
                left++;
            }
        }
        this.usedSize = left;
    }

    // 获取顺序表长度
    public int size() {
        return this.usedSize;
    }

    // 清空顺序表
    public void clear() {
        this.elem = new int[MyArraylist.DEFAULT_SIZE];
        this.usedSize = 0;
    }
}
